package Presentation;

import Metier.Controleurs.GestionStats;
import com.toedter.calendar.JDateChooser;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class OngletBilan extends Onglet implements ActionListener, PropertyChangeListener {

    @Override
    public void actionPerformed(ActionEvent ae) {
        rafraichir();
    }

    private final GestionStats gStats;
    private JScrollPane scrollTblGeneral;
    private JTable tblGeneral;

    private JPanel panelGauche;

    private ButtonGroup groupRadioButton;
    private JRadioButton rbJour;
    private JRadioButton rbMois;

    private JDateChooser dateChoix;

    ResourceBundle statBundle;

    /**
     * Constructeur
     * @param fp
     */
    public OngletBilan(FenetrePrincipale fp) {
        super(fp);
        gStats = new GestionStats(this);
        statBundle = ResourceBundle.getBundle("ressources/Stats/Stats", fp.getLocale());
        initComponents();
    }

    private void initComponents() {
        //Centre
        tblGeneral = new JTable() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        tblGeneral.setRowHeight(20);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        tblGeneral.setDefaultRenderer(Object.class, centerRenderer);

        scrollTblGeneral = new JScrollPane(tblGeneral);
        add(this.scrollTblGeneral, BorderLayout.CENTER);

        //Gauche
        panelGauche = new JPanel();
        panelGauche.setLayout(new GridLayout(12, 1));

        JPanel panelRadio = new JPanel();
        panelRadio.setLayout(new BoxLayout(panelRadio, BoxLayout.LINE_AXIS));
        rbJour = new JRadioButton(statBundle.getString("stats.parJour"));
        rbMois = new JRadioButton(statBundle.getString("stats.parMois"));

        rbJour.addActionListener(this);
        rbMois.addActionListener(this);
        panelRadio.add(rbJour);
        panelRadio.add(rbMois);

        groupRadioButton = new ButtonGroup();
        groupRadioButton.add(rbJour);
        groupRadioButton.add(rbMois);

        rbJour.setSelected(true);

        dateChoix = new JDateChooser(new Date());
        dateChoix.getDateEditor().addPropertyChangeListener(this);

        panelGauche.add(dateChoix);
        panelGauche.add(panelRadio);
        add(panelGauche, BorderLayout.WEST);

        rafraichir();
    }

    @Override
    public void rafraichir() {
        if (rbJour.isSelected()) {
            tblGeneral.setModel(gStats.getContenuDate(dateChoix.getCalendar(), Calendar.DATE, statBundle));
        } else {
            tblGeneral.setModel(gStats.getContenuDate(dateChoix.getCalendar(), Calendar.MONTH, statBundle));
        }
    }

    @Override
    public void help() {
        String msg = "<html>"
                + "<style>p {width:800px;}</style>"
                + "<h1>" + statBundle.getString("stats.aideMessage.titre") + "</h1>"
                + "<h2>" + statBundle.getString("stats.aideMessage.role.titre") + "</h2>"
                + "<p>" + statBundle.getString("stats.aideMessage.role.texte") + "</p>"
                + "<h2>" + statBundle.getString("stats.aideMessage.fonctions.titre") + "</h2>"
                + "<h3>" + statBundle.getString("stats.aideMessage.fonctions.selectionJour.titre") + "</h3>"
                + "<p>" + statBundle.getString("stats.aideMessage.fonctions.selectionJour.texte") + "</p>"
                + "<h3>" + statBundle.getString("stats.aideMessage.fonctions.parJour.titre") + "</h3>"
                + "<p>" + statBundle.getString("stats.aideMessage.fonctions.parJour.texte") + "</p>"
                + "<h3>" + statBundle.getString("stats.aideMessage.fonctions.parMois.titre") + "</h3>"
                + "<p>" + statBundle.getString("stats.aideMessage.fonctions.parMois.texte") + "</p>"
                + "<p>" + statBundle.getString("stats.aideMessage.footer") + "</p>"
                + "</html>";
        JOptionPane.showMessageDialog(this.getFP(),
                msg,
                statBundle.getString("stats.aide"),
                JOptionPane.INFORMATION_MESSAGE);
    }

    /**
     *
     * @param args
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(() -> {
                new FenetrePrincipale(null, 2, 1, Locale.FRENCH).setVisible(true);
        });
    }

    @Override
    public void propertyChange(PropertyChangeEvent pce) {
        rafraichir();
    }
}
